package com.example.controller;

import com.example.service.SysClusterService;
import org.jgroups.Address;
import org.jgroups.util.UUID;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class SysClusterController {

    Logger logger = org.slf4j.LoggerFactory.getLogger(SysClusterController.class);

    @Resource
    private SysClusterService sysClusterService;

    /***
     * 集群广播消息
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "sendAll")
    public String sendAll() throws Exception {
        String uuid = UUID.randomUUID().toString();
        sysClusterService.getSysClusterServer().sendAll(uuid);
        return uuid;
    }

    /***
     * 获取集群成员列表
     * @return
     */
    @RequestMapping(value = "getClusterMembers")
    public void getClusterMembers() {
        for (Address address : sysClusterService.getSysClusterServer().getChannel().getView().getMembers()) {
            logger.warn("address: " + address.toString());
        }
    }
}
